package cn.edu.sjtu.se.dslab.project;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.log4j.Logger;

@Entity
@Table(name = "bus_zone")
public class BusZone implements Serializable ,Cloneable{
	private static final Logger logger = Logger.getLogger(BusZone.class);

	private static final long serialVersionUID = 7959821682774869078L;

	private String name;
	private String entityZones;
	private long id;

	private Project project;

	@Column(nullable = false)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(nullable = false)
	public String getEntityZones() {
		return entityZones;
	}

	public void setEntityZones(String entityZones) {
		this.entityZones = entityZones;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	@ManyToOne(cascade = CascadeType.REFRESH)
	@JoinColumn(name = "project_id", nullable = false)
	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public void addEntityZones(String[] entityZoneIds) {
		if (entityZoneIds == null)
			return;
		StringBuilder sb = new StringBuilder();
		for (String id : entityZoneIds) {
			sb.append(id).append("|");
		}
		logger.info(sb.toString());
		setEntityZones(sb.toString());
	}
	
	@Override
	public Object clone() {
		BusZone bz = null;
		try{
			bz = (BusZone)super.clone();
			bz.setId(0);
			bz.setProject(null);
			bz.setEntityZones(null);
		}catch(CloneNotSupportedException ex){
			ex.printStackTrace();
		}
		return bz;
	}

}
